package com.danco.planner.view.core;

import java.util.ArrayList;
import java.util.List;

import com.danco.planner.supportclasses.enumeration.*;
import com.danco.planner.view.interfaces.IEventListener;

public class View {

	protected List<View> subviews;
	public List<View> getSubviews() {
		return subviews;
	}
	public boolean addSubview(View view) {
		
		if (view != null && view != this && !subviews.contains(view)) {
			subviews.add(view);
			if (view.getSuperview() != this) {
				view.setSuperview(this);
			}
			return true;
		}
		return false;
	}
	public void addSubviews(View ... vars) {
		
		for (View view : vars) {
			addSubview(view);
		}
	}
	public boolean removeSubview(View view) {
		
		if (view != null && subviews.contains(view)) {
			removeSubview(view);
			if (view.getSuperview() == this) {
				view.removeFromParentView();
			}
			return true;
		}
		return false;
	}

	protected View superview;
	public View getSuperview() {
		return superview;
	}
	public void setSuperview(View view) {
		
		if (view != null && superview != view && !subviews.contains(view)) {
			
			if (superview != null) {
				removeFromParentView();
			}
			superview = view;
			view.addSubview(this);
		}
	}
	public void removeFromParentView() {
		
		if (superview != null) {
			View superview = this.superview;
			this.superview = null;
			
			superview.removeSubview(this);
		}
	}
	
	protected List<IEventListener> listeners;
	public List<IEventListener> getListeners() {
		return listeners;
	}
	public boolean addListener(IEventListener listener) {
		
		if (listener != null && !listeners.contains(listener)) {
			listeners.add(listener);
			return true;
		}
		return false;
	}
	public boolean removeListener(IEventListener listener) {
		
		if (listener != null && listeners.contains(listener)) {
			listeners.remove(listener);
			return true;
		}
		return false;
	}
	public boolean hasActiveListener() {
		return (listeners.size() > 0);
	}

	protected IEventType targetEventType;
	public IEventType getTargetEventType() {
		return targetEventType;
	}
	public void setTargetEventType(IEventType type) {
		targetEventType = (type == null)?DefaultEventType.None:type;
	}
	
	public boolean visible;
	public boolean isVisible() {
		return visible;
	}
	public void setVisible(boolean visible) {
		this.visible = visible;
	}
	
	public boolean enable;
	public boolean isEnable() {
		return enable;
	}
	public void setEnable(boolean enable) {
		this.enable = enable;
	}
	
	
	public View() {
		
		this(DefaultEventType.None);
	}
	public View(IEventType type) {
		
		super();
		this.subviews = new ArrayList<View>();
		this.listeners = new ArrayList<IEventListener>();
		this.visible = true;
		this.enable = true;
		setTargetEventType(type);
	}

	
	public final void doActionWithType(IEventType type, String targetCommand) {
		
		if (this.enable) {
			doCustomActionWithType(type, targetCommand);
		}
	}
	protected void doCustomActionWithType(IEventType type, String targetCommand) {
		
		for (IEventListener eventListener : listeners) {
			eventListener.handleAction(this, type);
		}
		
		for (View view : subviews) {
			view.doActionWithType(type, targetCommand);
		}
	}
	
	/*
	 * Whenever override this method, call super.customShow() at the end.
	 */
	protected void customShow() {
		
		for (View view : subviews) {
			view.show();
		}
	}
	public final void show() {
		
		if (this.visible) {
			customShow();
		}
	}
}
